package com.weng.cloud.service8881.mock;

import com.cloud.api.mock.MockArgDto;
import com.cloud.api.mock.MockConstants;
import com.cloud.api.mock.MockResDto;
import com.weng.cloud.commons.base.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author: weng
 * @Date: 2022/6/11
 * @Description: com.weng.cloud.service8881.mock
 */
@Service
public class MockMain {

    @Resource
    private MockRpc mockRpc;

    public List<MockResDto> mockMain(@NotEmpty String... contents) throws ParseException {

        // 调用前 arr -> list
        List<MockArgDto> argDtos = this.preMain(contents);

        // 执行调用 -> arg -> res
        List<MockResDto> resDtos = mockRpc.rpc(argDtos);

        // 调用后 -> 倒序
        resDtos = this.endMain(resDtos);

        // 出参
        return resDtos;
    }

    private List<MockArgDto> preMain(String... contents) throws ParseException {
        List<MockArgDto> argDtos = new ArrayList<>();
        Date date = TimeUtil.toDate(MockConstants.text);
        for (String content : contents) {
            date = TimeUtil.nextMonth(date);
            MockArgDto argDto = null;
            argDto = MockArgDto.builder().content(content).date(date).build();
            argDtos.add(argDto);
        }
        return argDtos;
    }

    public List<MockResDto> endMain(List<MockResDto> resDtos) {
        Collections.reverse(resDtos);
        return resDtos;
    }
}
